import { Fields, TableAction } from "@/types/public";
import { Button, Popconfirm, Space } from "antd";
import React from "react";
import { EditOutlined, DeleteOutlined, EyeOutlined } from "@ant-design/icons";

// 模板数据结构定义
export interface Template {
  /** 唯一标识 */
  id: number;

  /** 厂商名称 */
  vendor: string;

  /** 一级分类 */
  firstCategory: string;

  /** 二级分类 */
  secondCategory: string;

  /** 模板名称 */
  name: string;

  /** 模板类型 */
  templateType?: string;

  /** 协议类型 */
  protocolType: string;

  /** 产品图片 */
  productImage?: any;

  /** 产品编号 */
  productNumber?: string;

  /** 数据上传周期 */
  dataCycle: string;

  /** 备注信息 */
  remark?: string;

  /** 创建时间 */
  createdAt?: string | Date;

  /** 更新时间 */
  updatedAt?: string | Date;
}

export const createTemplateField = (
  tableAction: TableAction<Template> = {}
): Fields[] => {
  return [
    {
      valueType: "number",
      inputType: "input",
      key: "id",
      title: "ID",
      disabled: true,
      dataIndex: "id",
      width: 80,
      isEdit: false,
      visible: true,
    },
    {
      valueType: "string",
      inputType: "input",
      title: "厂商名称",
      dataIndex: "vendor",
      key: "vendor",
      placeholder: "请输入厂商名称",
      rules: [
        { required: true, message: "厂商名称不能为空" },
        { max: 255, message: "厂商名称长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "一级分类",
      dataIndex: "firstCategory",
      key: "firstCategory",
      placeholder: "请输入一级分类",
      rules: [
        { required: true, message: "一级分类不能为空" },
        { max: 255, message: "一级分类长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "二级分类",
      dataIndex: "secondCategory",
      key: "secondCategory",
      placeholder: "请输入二级分类",
      rules: [
        { required: true, message: "二级分类不能为空" },
        { max: 255, message: "二级分类长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "模板名称",
      dataIndex: "name",
      key: "name",
      placeholder: "请输入模板名称",
      rules: [
        { required: true, message: "模板名称不能为空" },
        { max: 255, message: "模板名称长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "模板类型",
      dataIndex: "templateType",
      key: "templateType",
      placeholder: "请选择模板类型",
      initialValue: "私有模板",
      options: [
        { value: "私有模板", label: "私有模板" },
        { value: "公共模板", label: "公共模板" },
      ],
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "协议类型",
      dataIndex: "protocolType",
      key: "protocolType",
      placeholder: "请输入协议类型",
      rules: [
        { required: true, message: "协议类型不能为空" },
        { max: 255, message: "协议类型长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "upload",
      title: "产品图片",
      dataIndex: "productImage",
      key: "productImage",
      placeholder: "请上传产品图片",
      rules: [{ required: false }],
      // accept: "image/*",
    },
    {
      valueType: "string",
      inputType: "input",
      title: "产品编号",
      dataIndex: "productNumber",
      key: "productNumber",
      placeholder: "请输入唯一产品编号",
      rules: [
        { max: 255, message: "产品编号长度不能超过255字符" },
        {
          validator: async (_: any, value: any) => {
            if (value) {
              // 这里可以添加唯一性校验逻辑
              return Promise.resolve();
            }
            return Promise.resolve();
          },
        },
      ],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "数据上传周期",
      dataIndex: "dataCycle",
      key: "dataCycle",
      placeholder: "请选择数据上传周期",
      options: [
        { value: "每天", label: "每天" },
        { value: "每周", label: "每周" },
        { value: "每月", label: "每月" },
        { value: "实时", label: "实时" },
      ],
      rules: [{ required: true, message: "请选择数据上传周期" }],
    },
    {
      valueType: "string",
      inputType: "textarea",
      title: "备注信息",
      dataIndex: "remark",
      key: "remark",
      placeholder: "请输入备注信息",
      autoSize: { minRows: 2, maxRows: 4 },
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "创建时间",
      dataIndex: "createdAt",
      key: "createdAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "更新时间",
      dataIndex: "updatedAt",
      key: "updatedAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 200,
      render: (_: any, record: Template) => (
        <Space size="middle">
          <Button
            type="link"
            size="small"
            icon={<EyeOutlined />}
            onClick={() => tableAction.detail?.(record)}
          >
            详情
          </Button>
          <Button
            type="link"
            size="small"
            icon={<EditOutlined />}
            onClick={() => tableAction.edit?.(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定删除该模板？"
            description="删除模板将无法恢复"
            onConfirm={() => tableAction.delete?.([record.id])}
          >
            <Button type="link" danger icon={<DeleteOutlined />}>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
};
